import { BrowserRouter, Routes, Route, Navigate } from "react-router-dom";

//导入页面组件
import Login from "./view/Login";
import Layout from "./view/Latout";
import Role from "./view/Role/role";
import Admin from "./view/Admin/Admin";
import Mine from "./view/Mine/Mine";
import UpPwd from "./view/UpPwd/UpPwd";
import Register from "./view/Register/Register";
import Notice from "./view/Notice/Notice";
import SingleBed from "./view/SingleBed/SingleBed";
import DoubleBed from "./view/DoubleBed/DoubleBed";
import UserManage from "./view/UserManage/UserManage";

import { useEffect } from "react";
import { useDispatch } from "react-redux";
import { adminSlice } from "./redux/redux";
import { $getMine } from "./apis/adminApi";

function App() {
  const dispatch = useDispatch();
  const { setAdmin } = adminSlice.actions;
  useEffect(() => {
    if (sessionStorage.getItem("loginId")) {
      const loginAdmin = sessionStorage.getItem("loginId");

      $getMine(loginAdmin).then((r) => {
        dispatch(setAdmin(r[0]));
      });
    }
  }, []);
  return (
    <BrowserRouter>
      <Routes>
        <Route path="/" element={<Navigate to="./layout" />} />
        <Route path="/login" element={<Login />} />
        <Route path="/register" element={<Register />} />

        <Route path="/layout" element={<Layout />}>
          <Route path="role" element={<Role />}></Route>
          <Route path="admin" element={<Admin />} />
          <Route path="mine" element={<Mine />} />
          <Route path="pwd" element={<UpPwd />} />
          <Route path="notice" element={<Notice />} />
          <Route path="oneBed" element={<SingleBed />} />
          <Route path="doubleBed" element={<DoubleBed />} />
          <Route path="userManage" element={<UserManage />} />
        </Route>
      </Routes>
    </BrowserRouter>
  );
}

export default App;
